﻿#region Copyright

// Majesty of Omega - The next evolution of macro management space strategy
// 
// Copyright 2007-2009 André Claaßen
// 
// This file is protected by the terms and conditions of the
// Microsoft Community License (Ms-CL 1.1), a copy of which should
// have been distributed along with this software. If not,
// you may find the license information at the following URL:
// 
// http://www.microsoft.com/resources/sharedsource/licensingbasics/communitylicense.mspx

#endregion

using System.Windows;
using DesignByContract;

namespace Majesty_of_Omega.GUI.Pages.Game.Research.Parts
{
    /// <summary>
    /// Simple control, to display the research progress in a technology field
    /// </summary>
    public partial class ResearchedTechProgressBarControl
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ResearchedTechProgressBarControl"/> class.
        /// </summary>
        public ResearchedTechProgressBarControl()
        {
            this.InitializeComponent();
        }

        #region Dependency Property  Percentage 

        /// <summary>
        /// Dependency property Percentage
        /// </summary>
        public static readonly DependencyProperty PercentageProperty =
            DependencyProperty.Register("Percentage", typeof (int), typeof (ResearchedTechProgressBarControl), new UIPropertyMetadata(50, OnPercentageValueChanged));


        /// <summary>
        /// Gets or sets the Percentage 
        /// </summary>
        /// <value>The value.</value>
        public int Percentage
        {
            get { return (int) GetValue(PercentageProperty); }
            set { SetValue(PercentageProperty, value); }
        }

        #endregion

        /// <summary>
        /// Change the values of the research bar, to reflect the researched progress in percent
        /// </summary>
        /// <param name="d">instance of ResearchedTechProgressBarControl</param>
        /// <param name="e">the new and old values</param>
        private static void OnPercentageValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            int percentage = (int) e.NewValue;
            ResearchedTechProgressBarControl progressBarControl = (ResearchedTechProgressBarControl) d;
            progressBarControl.ShowPercentage(percentage);
        }

        /// <summary>
        /// Adjust the relative row widths from the two named grid rows.
        /// </summary>
        /// <param name="percentage">The percentage.</param>
        private void ShowPercentage(int percentage)
        {
            Require.IsTrue(percentage >= 0 && percentage <= 100, 
                     "The percentage value must be in a range between 0 and 100");

            this.Researched.Width = new GridLength(percentage, GridUnitType.Star);
            this.UnResearched.Width = new GridLength(100 - percentage, GridUnitType.Star);
        }
    }
}